package cn.yunhe.insurance.service.impl;


import cn.yunhe.insurance.dao.ComboDao;
import cn.yunhe.insurance.dao.InsurCateDao;
import cn.yunhe.insurance.entity.Combo;
import cn.yunhe.insurance.service.ComboService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 董稳钰
 * @date 2018/9/18 12:10
 */
@Service("comboService")
public class ComboServiceImpl implements ComboService {
    @Resource
    private ComboDao comboDao;
    @Resource
    private InsurCateDao insurCateDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean addCombo(Combo combo) {
        boolean flag = comboDao.addCombo(combo);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean updateCombo(Combo combo) {
        return comboDao.updateCombo(combo);
    }

    @Override
    public Page<Combo> findLikeCombos(Map<String, Object> cond) {
        Page<Combo> page = new Page<Combo>();
        int currPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        //获取当前页
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        //获取每页的大小
        page.setPageSize(pageSize);

        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        //计算出总页数
        page = PageHelper.startPage(currPageNum, pageSize);
        List<Combo> comboList = comboDao.findLikeCombos(cond);
        for (Combo c : comboList) {

            c.setList(insurCateDao.getInsurcateByComuuid(c.getComuuid()));
        }
        return page;
    }

    @Override
    public Combo getComboById(String cuuid) {
        Combo combo = comboDao.getComboById(cuuid);
        combo.setList(insurCateDao.getInsurcateByComuuid(cuuid));
        System.out.println(combo.getList());
        return combo;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean delCombo(String cuuid) {
        return comboDao.delCombo(cuuid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean delCombos(List<String> combos) {
        return comboDao.delCombos(combos);
    }

    @Override
    public boolean adoptCombo(String cuuid) {
        return false;
    }

    @Override
    public boolean refuseCombo(String cuuid) {
        return false;
    }

    @Override
    public List<Combo> getAllCombo() {
        return null;
    }

    @Override
    public List<Combo> getAllUseableCombo() {
        return null;
    }



    public ComboDao getComboDao() {
        return comboDao;
    }

    public void setComboDao(ComboDao comboDao) {
        this.comboDao = comboDao;
    }

    public InsurCateDao getInsurCateDao() {
        return insurCateDao;
    }

    public void setInsurCateDao(InsurCateDao insurCateDao) {
        this.insurCateDao = insurCateDao;
    }
}
